export default function(){
    function register(){

    }
    $.extend(register.prototype,{
        init : function(){
            this.btn = $(".btn");
            this.btn.on("click" , "input" , $.proxy(this.setData,this));
            this.getData();
        },
        getData : function(){
            this.iphone = $("#account");
            $(this.iphone).blur($.proxy(this.checkIphone,this));
            this.creat_password = $("#password");
            $(this.creat_password.blur($.proxy(this.checkPword,this)))
            this.confirm_password = $("#inter_code");
            $(this.confirm_password).blur($.proxy(this.confirm,this));
        },
        checkIphone : function(){
            var error     = $(".active .error-box .error");
            var ok        = $(".active .error-box .ok")
            var msg_error = $(".active .error-box .msg_error");
            var msg_tips  = $(".active .error-box .msg_tips");
            var reg = /^1[3456789]\d{9}$/ ;
           if(! reg.test(this.iphone.val())){
                $(error).attr("style","display:block");
                $(msg_error).attr("style","display:block");
                $(msg_tips).attr("style","display:none");
                this.statu_phone = false;
           }
           else{
                $(ok).attr("style","display:block");
                $(error).attr("style","display:none");
                $(msg_error).attr("style","display:none");
                $(msg_tips).attr("style","display:none");
                this.statu_phone = true;
           }
        },
        checkPword : function(){
            var error     = $(".set-password .error-box .error");
            var ok        = $(".set-password .error-box .ok");
            var msg_error = $(".set-password .error-box .msg_error");
            var msg_tips  = $(".set-password .error-box .msg_tips");
            var reg = /^(\w){6,20}$/ ;
            if( ! reg.test(this.creat_password.val())){
                $(error).attr("style","display:block");
                $(msg_error).attr("style","display:block");
                $(msg_tips).attr("style","display:none");
                this.statu_pwd = false;
            }
            else{
                $(ok).attr("style","display:block");
                $(error).attr("style","display:none");
                $(msg_error).attr("style","display:none");
                $(msg_tips).attr("style","display:none");
                this.statu_pwd = true;
            }
        },
        confirm : function(){
            var error     = $(".error_box .error-box .error");
            var ok        = $(".error_box .error-box .ok");
            var msg_error = $(".error_box .error-box .msg_error");
            var value = this.creat_password.val();
            var confirm_value = this.confirm_password.val();
            if(value === confirm_value){
                $(ok).attr("style","display:block");
                $(msg_error).attr("style","display:none");
                $(error).attr("style","display:none");
                this.statu_cPwd = true;
            }
            if(confirm_value === "" || value !== confirm_value){
                $(msg_error).attr("style","display:block");
                $(error).attr("style","display:block");
                $(ok).attr("style","display:none");
                this.statu_cPwd = false;
            }
        },
        setData : function(){
            var ls = localStorage.getItem("user");
            if(ls === null){
                if(this.statu_phone === true && this.statu_pwd === true && this.statu_cPwd === true){
                    var user_phone = this.iphone.val();
                    var user_pwd   = this.creat_password.val();
                    var state = true;
                    var list = [{
                        user_phone,
                        user_pwd ,
                        state    , 
                    }]
                    localStorage.setItem("user",JSON.stringify(list));
                    if(window.confirm("注册成功")){
                        location.href ="http://localhost/VSCode/JuanPi_web_project/src/index.html";
                    }
                }
            }
            else{
                var user_phone = this.iphone.val();
                var user_pwd   = this.creat_password.val();
                var la = JSON.parse(ls);
                var has_same_id = false;
                $.each(la,function(index,item){
                    if(item.user_phone === user_phone ){
                        has_same_id = true;
                        alert("该用户名已存在");
                    }
                })
                // 不同名时
                if(!has_same_id){
                    la.push({
                        user_phone : user_phone ,
                        user_pwd   : user_pwd ,
                        state      :  true 
                    })
                    if(window.confirm("注册成功")){
                        location.href ="http://localhost/VSCode/JuanPi_web_project/src/index.html";
                    }
                }
                localStorage.setItem("user",JSON.stringify(la));
            }
        }
    })
    return new register()
}